/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.nhc.model;

import android.content.Context;
import android.content.SharedPreferences;

import com.astrob.nhcmap.R;

import zuo.biao.library.base.BaseModel;

public class CourseInfo extends BaseModel {

    private static final long serialVersionUID = 1L;

//    显示   更改数据区中航向的显示方式，以文字东、西、南、北、东南、西南.......或以角度的方式显示。
//    北向基准，更改航向以磁北为基准或真北为基准。

    //航向设置
    private static String COUREINFO = "coureinfo";

    private static String COURE_SHOWMODE = "coure_showmode";//航向显示模式
    private static String COURE_BXJZMODE = "coure_bxjzmode";//北向基准
    private static String COURE_LPVEL = "coure_lpvel";//GPS方向使用最低速度
    private static String COURE_LPCT = "coure_lpct";//GPS方向使用持续时间


    public static String[] SHOWMODE_NAMES = {"文字", "角度"};
    public static String[] BXJZMODE_NAMES = {"真北", "磁北"};

    public static final int[] VELSET_C = {2, 5, 10};
    public static final int[] CTIMESET_C = {1, 2, 3, 4, 5};

    private int showmode = 0;//航向显示 0-文字 1-角度
    private int bxjzmode = 0;//北向基准 0-真北 1-磁北
    private int lpvel = 1;//km/h
    private int lpct = 1;//


    private static CourseInfo instance = new CourseInfo();

    public static CourseInfo getInstance() {
        return instance;
    }

    /**
     * 默认构造方法，JSON等解析时必须要有
     */
    public CourseInfo() {
        //default
    }

    public void save(Context context) {
        if (context == null) {
            return;
        }

        SharedPreferences sp = context.getSharedPreferences(
                COUREINFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(COURE_SHOWMODE, this.showmode);
        editor.putInt(COURE_BXJZMODE, this.bxjzmode);
        editor.putInt(COURE_LPVEL, this.lpvel);
        editor.putInt(COURE_LPCT, this.lpct);
        editor.commit();
    }

    public void load(Context context) {
        if (context == null) {
            return;
        }

        SHOWMODE_NAMES = context.getResources().getStringArray(R.array.course_showmode_names);
        BXJZMODE_NAMES = context.getResources().getStringArray(R.array.course_bxjzmode_names);

        SharedPreferences sp = context.getSharedPreferences(
                COUREINFO, Context.MODE_PRIVATE);
        this.showmode = sp.getInt(COURE_SHOWMODE, this.showmode);
        this.bxjzmode = sp.getInt(COURE_BXJZMODE, this.bxjzmode);
        this.lpvel = sp.getInt(COURE_LPVEL, this.lpvel);
        this.lpct = sp.getInt(COURE_LPCT, this.lpct);
    }

    public void reset() {
        showmode = 0;//航向显示 0-文字 1-角度
        bxjzmode = 0;//北向基准 0-真北 1-磁北
        lpvel = 1;//
        lpct = 1;//
    }

    long _begintime = 0;//满足条件的初始时间

    //gpsspeed  km／h
    public boolean isUseGpsdir(float gpsspeed) {
        long c = System.currentTimeMillis();
        int vel = getLpvelNum();
        if (gpsspeed > vel) {
            if (_begintime == 0) {
                _begintime = c;
            } else if ((c - _begintime) / 1000 >= getLpctNum()) {
                return true;
            }
        } else {
            _begintime = 0;
        }

        return false;
    }

    public int getLpvelNum() {
        if (lpvel < 0 || lpvel >= VELSET_C.length) {
            lpvel = 0;
        }
        return VELSET_C[lpvel];
    }


    public int getLpctNum() {
        if (lpct < 0 || lpct >= CTIMESET_C.length) {
            lpct = 0;
        }
        return CTIMESET_C[lpct];
    }

    public String getLpvelStr() {
        return getLpvelNum() + "km/h";
    }

    public String getLpctStr() {
        return getLpctNum() + "s";
    }

    public String getShowmodeStr() {
        if (showmode < 0 || showmode >= SHOWMODE_NAMES.length) {
            showmode = 0;
        }
        return SHOWMODE_NAMES[showmode];
    }

    public String getBXJZmodeStr() {
        if (bxjzmode < 0 || bxjzmode >= BXJZMODE_NAMES.length) {
            bxjzmode = 0;
        }
        return BXJZMODE_NAMES[bxjzmode];
    }

    public int getShowmode() {
        return showmode;
    }

    public void setShowmode(int showmode) {
        this.showmode = showmode;
    }

    public int getBxjzmode() {
        return bxjzmode;
    }

    public void setBxjzmode(int bxjzmode) {
        this.bxjzmode = bxjzmode;
    }

    public int getLpvel() {
        return lpvel;
    }

    public void setLpvel(int lpvel) {
        this.lpvel = lpvel;
    }

    public int getLpct() {
        return lpct;
    }

    public void setLpct(int lpct) {
        this.lpct = lpct;
    }

    @Override
    protected boolean isCorrect() {//根据自己的需求决定，也可以直接 return true
        return id > 0;// && StringUtil.isNotEmpty(phone, true);
    }

}
